package play.go.controller;

import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.memcache.Memcache;

import play.go.GoServer;
import play.go.model.Player;

/**
 * Join to go channel server.
 * @author shuji.w6e
 * @since 0.1.0
 */
public class JoinController extends AbstructGoController {

    @Override
    public Navigation run() throws Exception {
        String userName = super.asString("user_name");
        if (userName == null || userName.length() == 0) {
            // TODO
        }
        // TODO now guest only
        Player player = new Player();
        player.setName(userName);
        Datastore.put(player);
//        Key key = Datastore.createKey(PlayerMeta.get(), id);
//        Player p = Datastore.getOrNull(PlayerMeta.get(), key);
//        if (p == null) {
//            p = new Player();
//            p.setKey(key);
//            p.setName(id);
//        }
        String channelId = GoServer.createChannel(player);
        Memcache.put(channelId, player);
        System.out.println("join: " + channelId + ", player=" + player);
        return text(channelId);
    }
}
